<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/2/5
 * Time: 15:59
 */

namespace App\Http\Controllers\Api;

use App\Exceptions\ApiException;
use App\Helpers\Api\ApiResponse;
use App\Http\Controllers\Controller;
use App\User;
use EasyWeChat\Factory;
use HttpException;

class ApiBase extends Controller
{
    use ApiResponse;

    /**
     * @return User|bool
     */
    protected function getUser()
    {
        $user = auth('api')->user();
        if ($user) {
            return $user;
        } else {
            return false;
        }
    }

    /**
     * @param string $key
     * @return mixed
     * @throws ApiException
     */
    protected function getId($key = 'id', $throw = true)
    {
        $id = hashid_decode(request($key));
        if ($id == 0 && $throw) {
            throw new ApiException($key.'参数错误');
        }
        return $id;
    }

    /**
     * @return array|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    protected function getJsSdkConfig()
    {
        try {
            /*$is_ios = request()->header('IsIos');
            $in_url = request()->header('InUrl');
            $in_url = explode("#", $in_url)[0];
            if ($is_ios == 'true') {
                $url = $in_url;
            } else {

            }*/
            $url = url()->previous();
            $config = $this->getWXGzhConfig();
            $app = Factory::officialAccount($config);
            $arr = $app->jssdk->setUrl($url)->buildConfig(['chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'getLocalImgData'], false, false, false);
            $arr['url'] = url()->previous();
            return $arr;
        } catch (HttpException $exception) {
            return [];
        }
    }



}